<template>
  <c-form-dialog
    ref="form"
    :visible.sync="innerVisible"
    :form-items="formItems"
    :form-data="formData"
    :table-name="tableName"
    :table-id="tableId"
    @close="handleClose"
    @success="handleSuccess"
    :before-save="handleBeforeSave"
  ></c-form-dialog>
</template>

<script>
import { enumProductType } from '@/credit/views/province/goods/goodsMixins'
import { mapGetters } from 'vuex'

// import SelectProductCategory from '@/credit/views/province/goods/components/SelectProductCategory'
import SelectArea from '@/credit/components/select/SelectArea'
import SelectProductGuaMethod from '@/credit/components/select/SelectProductGuaMethod'
import SelectProductFeatures from '@/credit/components/select/SelectProductFeatures'
import SelectProductType from '@/credit/views/province/goods/components/SelectProductType'
export default {
  components: {},
  props: {
    visible: Boolean,
    tableId: String,
    formData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    const isNumberValidator = (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (!Number.isFinite(+value)) {
        callback(new Error('请输入正确的数值'))
      } else {
        callback()
      }
    }
    return {
      innerVisible: false,
      tableName: 'industrial_package',
      supplyProductName: '',
      formItems: [
        {
          tag: 'input',
          itemAttrs: {
            label: '产品名',
            rules: [{ required: true, message: '请输入产品名', trigger: 'blur' }],
          },
          attrs: { key: 'load_name', value: '', placeholder: '请输入产品名' },
        },
        {
          tag: 'radio-group',
          itemAttrs: {
            label: '产品类型',
            // rules: [{ required: true, message: '请选择产品类型', trigger: 'change' }],
          },
          attrs: {
            clearable: false,
            key: 'product_type',
            value: '1',
            options: enumProductType,
          },
        },
        {
          tag: SelectProductType,
          itemAttrs: {
            label: '表单类型',
            // rules: [{ required: true, message: '请选择反担保方式', trigger: 'change' }],
          },
          attrs: {
            value: '',
            key: 'form_type',
            class: 'form-control-ml',
          },
        },
        {
          tag: SelectProductGuaMethod,
          itemAttrs: {
            label: '反担保方式',
            // rules: [{ required: true, message: '请选择反担保方式', trigger: 'change' }],
          },
          attrs: {
            value: '',
            key: 'gua_method',
            class: 'form-control-ml',
          },
          listeners: {
            select: v => {
              this.guaMethodName = v
            },
          },
        },
        {
          tag: 'textarea',
          itemAttrs: {
            label: '支持产业',
            // rules: [{ required: true, message: '请输入支持产业', trigger: 'blur' }],
          },
          attrs: { key: 'supply_product_name', value: '', placeholder: '请输入支持产业' },
          // tag: SelectProductCategory,
          // itemAttrs: {
          //   label: '支持产业',
          //   rules: [{ required: true, message: '请选择支持产业', trigger: 'blur' }],
          // },
          // attrs: {
          //   key: 'supply_product',
          //   value: '',
          //   placeholder: '请选择支持产业（多选）',
          //   class: 'form-control-ml',
          //   clearable: true,
          // },
          // listeners: {
          //   select: v => {
          //     this.supplyProductName = v
          //   },
          // },
        },
        {
          tag: SelectArea,
          itemAttrs: {
            label: '适用区域',
            // rules: [{ required: true, message: '请选择适用区域', trigger: 'change' }],
          },
          attrs: {
            key: 'supply_address_code',
            value: '',
            placeholder: '请选择适用区域',
            class: 'form-control-ml',
          },
          listeners: {
            change: (r, o) => (this.addressNames = (o || []).map(it => it.geo_name).join(',')),
          },
        },
        {
          tag: 'input',
          itemAttrs: { label: 'supply_address_code_name' },
          attrs: { key: 'supply_address_code_name', value: '' },
          ifShow: false,
          getValue: () => this.addressNames,
        },

        {
          tag: 'textarea',
          itemAttrs: {
            label: '适用对象',
            // rules: [{ required: true, message: '请输入适用对象', trigger: 'blur' }],
          },
          attrs: { key: 'load_entity', value: '', placeholder: '请输入适用对象' },
        },

        {
          tag: 'number',
          itemAttrs: {
            label: '担保额度最小值',
            rules: [
              // { required: true, message: '请输入担保额度最小值', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
            ],
          },
          attrs: { key: 'load_amount_min', value: '', placeholder: '请输入担保额度最小值' },
          tagSlots: [{ name: 'append', slot: '万元' }],
        },
        {
          tag: 'number',
          itemAttrs: {
            label: '担保额度最大值',
            rules: [
              // { required: true, message: '请输入担保额度最大值', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
            ],
          },
          attrs: { key: 'load_amount', value: '', placeholder: '请输入担保额度最大值' },
          tagSlots: [{ name: 'append', slot: '万元' }],
        },
        {
          tag: 'number',
          itemAttrs: {
            label: '融资期限最小值',
            rules: [
              // { required: true, message: '请输入融资期限最小值', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
            ],
          },
          attrs: { key: 'loan_term_min', value: '', placeholder: '请输入融资期限最小值' },
          tagSlots: [{ name: 'append', slot: '月' }],
          directives: [
            {
              name: 'input-number',
              value: {
                min: 0,
                fixed: 0,
              },
            },
          ],
        },
        {
          tag: 'number',
          itemAttrs: {
            label: '融资期限最大值',
            rules: [
              // { required: true, message: '请输入融资期限最大值', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
            ],
          },
          attrs: { key: 'loan_term', value: '', placeholder: '请输入融资期限最大值' },
          tagSlots: [{ name: 'append', slot: '月' }],
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '担保费率',
            // rules: [{ required: true, message: '请输入担保费率', trigger: 'blur' }],
          },
          attrs: { key: 'gua_rate', value: '', placeholder: '请输入担保费率' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '贷款利率',
            // rules: [{ required: true, message: '请输入贷款利率', trigger: 'blur' }],
          },
          attrs: { key: 'bank_rate', value: '', placeholder: '请输入贷款利率' },
        },
        {
          tag: 'textarea',
          itemAttrs: {
            label: '产品简介',
          },
          attrs: { key: 'des', value: '', placeholder: '请输入产品简介' },
        },
        {
          tag: SelectProductFeatures,
          itemAttrs: {
            label: '产品特点',
          },
          attrs: {
            key: 'features_key',
            value: '',
            placeholder: '请选择产品特点',
            class: 'form-control-ml',
          },
          listeners: {
            select: v => {
              this.productFeatures = v
            },
          },
        },
        {
          tag: 'textarea',
          itemAttrs: {
            label: '禁入条件',
          },
          attrs: { key: 'debar', value: '', placeholder: '请输入禁入条件' },
        },
        {
          tag: 'textarea',
          itemAttrs: {
            label: '准入条件',
          },
          attrs: { key: 'access_cond', value: '', placeholder: '请输入准入条件' },
        },
        {
          tag: 'textarea',
          itemAttrs: {
            label: '反担保措施',
          },
          attrs: { key: 'risk_measure', value: '', placeholder: '请输入反担保措施' },
        },
        {
          tag: 'textarea',
          itemAttrs: {
            label: '办理流程',
          },
          attrs: { key: 'process_des', value: '', placeholder: '请输入办理流程' },
        },
        {
          tag: 'textarea',
          itemAttrs: {
            label: '资料清单',
          },
          attrs: { key: 'submit_list', value: '', placeholder: '请输入资料清单' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '联系电话',
          },
          attrs: { key: 'concat_number', value: '' },
        },
      ],
      addressNames: '',
      guaMethodName: '',
      productFeatures: '',
    }
  },
  computed: {
    ...mapGetters(['user', 'clientId']),
    isCounty() {
      return this.user.orgRegionLever > 2
    },
  },
  watch: {
    formData: {
      handler: function (data) {
        this.addressNames = data.supply_address_code_name
        this.guaMethodName = data.gua_method_name
        this.productFeatures = data.features
      },
      immediate: true,
      deep: true,
    },
    visible(val) {
      this.innerVisible = val
    },
  },
  created() {},
  mounted() {},
  methods: {
    handleBeforeSave(model) {
      model.features = this.productFeatures
      model.gua_method_name = this.guaMethodName
      model.form_type = model.form_type || null
      // model.supply_product_name = this.supplyProductName || model.supply_product_name
      model.service_id = this.clientId
      model.supply_agency_id = this.clientId
      model.is_county = 1
      model.finace_channel = 1

      return model
    },
    handleClose() {
      this.$emit('update:visible', false)
    },
    handleSuccess(res) {
      this.$emit('success', res)
    },
  },
}
</script>
<style lang="scss" scoped></style>
